<?php
// +-------------------------------------------------------------------------------
// | Hermales v2.2
// +-------------------------------------------------------------------------------
// | 扩展类库
// |
// | Copyright (c) 2015 http://www.heimalanshi.com All rights reserved.
// | Author : JiangJun
// +-------------------------------------------------------------------------------
namespace sms;

use common\Http;

/**
 * 互亿无线客户端短信类库扩展
 *
 * @author ZhuJie
 *
 */
class ResourceSmsHelperImpl implements SmsHelper {
    const SERVER = "http://resource.heimalanshi.com";
    private $guestName;
    private $key;

    /**
     * 构造函数
     *
     * ResourceSmsHelperImpl constructor.
     * @param $guestName
     * @param $key
     */
    public function __construct($guestName, $key) {
        $this->guestName = $guestName;
        $this->key = $key;
    }

    /**
     * 处理响应内容
     *
     * @param $response
     * @return array|mixed
     */
    private static function _processResponse($response) {
        if (is_array($response)) {
            return $response;
        }
        $responseArray = json_decode($response, true);
        if ($responseArray == null) {
            //如果返回null，可能的原因是后台相关数据表没有创建
            return [
                'status' => 0,
                'info' => '解析失败：' . $response
            ];
        }
        return $responseArray;
    }

    /**
     * 发送短信验证码
     *
     * @param string $mobile
     * @param int $expired
     * @param bool $code
     * @param string $type
     * @param int $uid
     * @param bool $message
     * @return array|mixed
     */
    public function sendVerify($mobile,
                               $expired = 600,
                               $code = false,
                               $type = "",
                               $uid = -1,
                               $message = false) {
        if (!$message) {
            $message = SmsHelper::DEFAULT_VERIFY_CODE_MESSAGE;
        }
        $data = array(
            'guest' => $this->guestName,
            'key' => $this->key,
            'phone' => $mobile,
            'uid' => $uid,
            'type' => $type,
            'message' => $message,
            'expired' => $expired
        );
        if ($code) {
            $data['code'] = $code;
        }

        $response = Http::postForm(self::SERVER . "/sms/index/sendVerify", $data);

        return self::_processResponse(json_decode($response, true));
    }

    /**
     * 检查短信验证码
     *
     * @param string $phone
     * @param string $code
     * @param string $type
     * @param int $uid
     * @return array|mixed
     */
    function checkVerify($phone, $code, $type = "", $uid = -1) {
        $data = array(
            'guest' => $this->guestName,
            'key' => $this->key,
            'phone' => $phone,
            'uid' => $uid,
            'type' => $type,
            'code' => $code
        );
        $response = Http::postForm(self::SERVER . "/sms/index/checkVerify", $data);
        return self::_processResponse($response);
    }

    /**
     * 发送短信
     *
     * @param string $mobile
     * @param string $message
     * @return array|mixed
     */
    function send($mobile, $message) {
        $data = array(
            'guest' => $this->guestName,
            'key' => $this->key,
            'phone' => $mobile,
            'message' => $message
        );
        $ret = Http::postForm(self::SERVER . "/sms/index/send", $data);
        return self::_processResponse($ret);
    }
}

?>